//
//  Algorithm_BubbleSort_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/10/10.
//

import UIKit

class Algorithm_BubbleSort_DemoVC: Base_TableVC {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        let nums = [3, 5, 15, 26, 2, 27, 4, 19, 36, 47, 50, 48, 38, 44, 46]
        /// "originalNums is [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]"
        
//        let nums = [3]
        /// "originalNums is [3]"
        
        let nums = [26, 2]
        /// "originalNums is [2, 26]"
        
        bubbleSort(nums)
    }
    
    
    @discardableResult
    private func bubbleSort(_ nums: [Int]) -> [Int] {
        guard !nums.isEmpty else {
            return []
        }
        var originalNums = nums
        for i in 0..<originalNums.count {
            for j in 0..<originalNums.count-i-1 {
                if originalNums[j] > originalNums[j+1] {
                    let temp = originalNums[j]
                    originalNums[j] = originalNums[j+1]
                    originalNums[j+1] = temp
                }
            }
        }
        debugPrint("originalNums is \(originalNums)")
        return originalNums
    }

}
